<template>
	<div class="login-wrap">
		<div class="ms-login">
			<div class="ms-title">快递配送系统 - 注册</div>
			<el-form :model="param" :rules="rules" ref="registerFormRef" label-width="0px" class="ms-content">
				<el-form-item prop="name">
					<el-input v-model="param.name" placeholder="姓名">
						<template #prepend>
							<el-button :icon="Avatar"></el-button>
						</template>
					</el-input>
				</el-form-item>
				<el-form-item prop="phone">
					<el-input v-model="param.phone" placeholder="账号(手机号码)">
						<template #prepend>
							<el-button :icon="User"></el-button>
						</template>
					</el-input>
				</el-form-item>
				<el-form-item prop="password">
					<el-input
						type="password"
						placeholder="密码"
						v-model="param.password"
					>
						<template #prepend>
							<el-button :icon="Lock"></el-button>
						</template>
					</el-input>
				</el-form-item>
				<el-form-item prop="password">
					<el-input
							type="password"
							placeholder="确认密码"
							v-model="param.rePwd"
							@keyup.enter="submitForm(login)"
					>
						<template #prepend>
							<el-button :icon="Lock"></el-button>
						</template>
					</el-input>
				</el-form-item>
				<div class="login-btn">
					<el-button type="primary" @click="submitForm(registerFormRef)">注册</el-button>
				</div>

				<el-button type="text" @click="goLogin">已有账号？前往登录</el-button>
			</el-form>
		</div>
	</div>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue';
import { useTagsStore } from '../store/tags';
import { usePermissStore } from '../store/permiss';
import { useRouter } from 'vue-router';
import { ElMessage } from 'element-plus';
import type { FormInstance, FormRules } from 'element-plus';
import { Lock, User,Avatar,Iphone } from '@element-plus/icons-vue';
import { registerApi } from '../api/index';

interface RegisterInfo {
	name: string;
	phone: string;
	password: string;
	rePwd: string;
}

const router = useRouter();
const param = reactive<RegisterInfo>({
	name: '',
	phone: '',
	password: '',
	rePwd: ''
});

const rules: FormRules = {
	name: [{required: true, message: '请输入姓名', trigger: 'blur'}],
	phone: [{required: true, message: '请输入账号(手机号码)', trigger: 'blur'}],
	password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
	rePwd: [{ required: true, message: '请输入确认密码', trigger: 'blur' }]
};
const permiss = usePermissStore();
const registerFormRef = ref<FormInstance>();
const submitForm = async (formEl: FormInstance | undefined) => {
	if (!formEl) return;
	await formEl.validate(async (valid: boolean) => {
		console.log("register form");
		if (valid) {
			if (param.password != param.rePwd) {
				ElMessage.warning('密码与确认密码不一致');
				return ;
			}
			const registerResult = await registerApi(param);
			if (registerResult) {
				await router.push('/login');
				ElMessage.success('注册成功，请登录');
			}
		} else {
			return false;
		}
	});
};

const goLogin = async () => {
	await router.push("/login");
};
</script>

<style scoped>
.login-wrap {
	position: relative;
	width: 100%;
	height: 100%;
	background-image: url(../assets/img/login-background.png);
	background-size: 100%;
}
.ms-title {
	width: 100%;
	line-height: 50px;
	text-align: center;
	font-size: 20px;
	color: #fff;
	border-bottom: 1px solid #ddd;
}
.ms-login {
	position: absolute;
	left: 50%;
	top: 50%;
	width: 350px;
	margin: -190px 0 0 -175px;
	border-radius: 5px;
	background: rgba(255, 255, 255, 0.3);
	overflow: hidden;
}
.ms-content {
	padding: 30px 30px;
}
.login-btn {
	text-align: center;
}
.login-btn button {
	width: 100%;
	height: 36px;
	margin-bottom: 10px;
}
.login-tips {
	font-size: 12px;
	line-height: 30px;
	color: #fff;
}
</style>
